﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MySql.Data.MySqlClient;

namespace RestaurantTabletApplication
{
    class OrderManager
    {
        private const string placeOrderSql = "INSERT INTO meal_order(tableNumber, seatNumber, waiterName, mealNumber, orderStatus) VALUES({0}, {1}, '{2}', {3}, '{4}')";
        private const string getByStatusSql = "SELECT * FROM meal_order WHERE orderStatus = '{0}'";
        private const string setStatusSql = "UPDATE meal_order SET orderStatus = '{0}' WHERE orderNumber = {1}";
        private const string getPlacedOrderSql = "SELECT * FROM meal_order WHERE tableNumber = {0} AND seatNumber = {1} AND orderStatus != 'Delivered'";
        private const string getPlacedOrdersSql = "SELECT * FROM meal_order WHERE tableNumber = {0} AND orderStatus != 'Delivered'";

        private DatabaseManager dbManager;

        public OrderManager()
        {
            dbManager = new DatabaseManager();
        }

        public int placeOrder(Order order)
        {
            dbManager.executeNonQuery(String.Format(placeOrderSql, order.TableNumber, order.SeatNumber, order.WaiterName, order.Meal.MealNumber, OrderStatus.Placed.ToString()));
            return Int32.Parse(dbManager.executeScalar("SELECT LAST_INSERT_ID()").ToString());
        }

        public Order getActiveOrder(int table, int chair)
        {
            MySqlConnection conn = null;
            MySqlDataReader reader = null;
            try
            {
                conn = dbManager.getConnection();
                reader = dbManager.getDataReader(String.Format(getPlacedOrderSql, table, chair), conn);
                List<Order> orders = createOrderList(reader);
                if (orders.Count > 0)
                {
                    return orders[0];
                }
                return null;
            }
            finally
            {
                DatabaseManager.close(reader);
                DatabaseManager.close(conn);
            }
        }

        public List<Order> getActiveOrders(int table)
        {
            MySqlConnection conn = null;
            MySqlDataReader reader = null;
            try
            {
                conn = dbManager.getConnection();
                reader = dbManager.getDataReader(String.Format(getPlacedOrdersSql, table), conn);
               
                return createOrderList(reader);
            }
            finally
            {
                DatabaseManager.close(reader);
                DatabaseManager.close(conn);
            }
        }

        public List<Order> getOrdersToBeDelivered()
        {
            return getByStatus(OrderStatus.Ready);
        }

        public void acknowledgeDelivery(Order order)
        {
            setStatus(order.OrderNumber, OrderStatus.Delivered);
        }

        public List<Order> getPendingOrders()
        {
            return getByStatus(OrderStatus.Placed);
        }

        public void indicateReady(Order order)
        {
            setStatus(order.OrderNumber, OrderStatus.Ready);
        }

        private void setStatus(int orderNumber, OrderStatus status)
        {
            dbManager.executeNonQuery(String.Format(setStatusSql, status.ToString(), orderNumber));
        }

        private List<Order> getByStatus(OrderStatus status)
        {
            MySqlConnection conn = null;
            MySqlDataReader reader = null;
            try
            {
                conn = dbManager.getConnection();
                reader = dbManager.getDataReader(String.Format(getByStatusSql, status.ToString()), conn);
                return createOrderList(reader);
            }
            finally
            {
                DatabaseManager.close(reader);
                DatabaseManager.close(conn);
            }
        }

        private List<Order> createOrderList(MySqlDataReader reader)
        {
            List<Order> orders = new List<Order>();
            while (reader.Read())
            {
                Order order = new Order();
                order.OrderNumber = (int)reader["orderNumber"];
                order.TableNumber = (int)reader["tableNumber"];
                order.SeatNumber = (int)reader["seatNumber"];
                order.Meal = ClientMenu.Instance.getMealByNumber((int)reader["mealNumber"]);
               // order.OrderTime = (DateTime)reader["orderTime"];
                order.Status = (OrderStatus)Enum.Parse(typeof(OrderStatus), (string)reader["orderStatus"], false);
                orders.Add(order);
            }
            return orders;
        }
    }
}
